using UnityEngine;
namespace DefaultNamespace.Pool
{
    public class ListNode<T>
    {
        public T Data { get; set; }
        public ListNode<T> Next { get; set; }
        
        public ListNode(T data)
        {
            Data = data;
            Next = null;
        }
    }
    //单链表类
    public class SinglyLinkedList<T>
    {
        private ListNode<T> head;
        
        // 在链表头部添加节点
        public void AddFirst(T data)
        {
            ListNode<T> newNode = new ListNode<T>(data);
            newNode.Next = head;
            head = newNode;
        }
        
        // 在链表尾部添加节点
        public void AddLast(T data)
        {
            ListNode<T> newNode = new ListNode<T>(data);
            
            if (head == null)
            {
                head = newNode;
                return;
            }
            
            ListNode<T> current = head;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = newNode;
        }
        
        // 删除指定数据的节点
        public bool Remove(T data)
        {
            if (head == null) return false;
            
            if (head.Data.Equals(data))
            {
                head = head.Next;
                return true;
            }
            
            ListNode<T> current = head;
            while (current.Next != null)
            {
                if (current.Next.Data.Equals(data))
                {
                    current.Next = current.Next.Next;
                    return true;
                }
                current = current.Next;
            }
            
            return false;
        }
        
        // 打印链表内容
        public void PrintList()
        {
            ListNode<T> current = head;
            while (current != null)
            {
                Debug.Log(current.Data + " -> ");
                current = current.Next;
            }
            Debug.Log("null");
        }
    }
    
}